<!--
 * @Description: 基础告警配置
-->
<template>
  <MainSlot>
    <template v-slot:searchForm>
      <!-- <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm> -->
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <el-table ref="tableData" v-loading="loading" :data="tableData" stripe border height="88%" @row-click="handleEdit"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="fishPondName" label="鱼塘名称" />
        <el-table-column prop="baseAlarmName" label="告警名称" />
        <el-table-column prop="dataItemName" label="告警数据项" />
        <!-- <el-table-column prop="alarmCharge" label="负责人" width="120" /> -->
        <el-table-column prop="alarmCharge" label="负责人" width="120" />
        <el-table-column prop="dataTypeName" label="告警数据类型" />
        <el-table-column prop="dataItemName" label="监测数据项" />
        <el-table-column width="200" label="操作">
          <template v-if="scope.row.baseAlarmId" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" 
              @click="dbHandleSee(scope.row)">
              <i class="el-icon-view"></i> 查看
            </el-button>
            <el-button class="text-btn-cursor" type="text" size="small" @click="edit">
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button class="text-btn-cursor cursor-delete" type="text" size="small"
              v-if="judgePermission(22, 'delete')" @click="handleDelete(scope.row)">
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="changeCurrentPage" :page="currentPage" :size="pageSize" :total="totalSize">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <el-dialog :close-on-click-modal='false' :title="titleText + '基础告警配置'" :visible.sync="dialogFormVisible"
        :before-close="cancel" width="50%">
        <el-form ref="formData"  :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item v-if="nowChanceStau === 'add'" label="模板选择" >
            <el-select class="w100p" filterable clearable v-model="nowalarmTemplateId" :disabled="!canInput" @change="chancetemple">
              <el-option v-for="(item, index) in alarmTemplatelist" :key="index" :value="item.alarmTemplateId"
                :label="item.templateName" />
            </el-select>
          </el-form-item>
          <el-form-item label="鱼塘选择" prop="fishPondObj">
            <el-select class="w100p" filterable clearable v-model="formData.fishPondObj" @change="chanbatch" value-key="fishPondId"
              placeholder="请选择鱼塘" :disabled="nowChanceStau == 'edit'">
              <el-option v-for="item in fishPondArr" :key="item.fishPondId" :label="item.pondName"
                :value="{ fishPondId: item.fishPondId, pondName: item.pondName }" />
            </el-select>
          </el-form-item>
          <el-form-item label="告警名称"  prop="baseAlarmName">
            <el-input v-model="formData.baseAlarmName" :disabled="!canInput" />
          </el-form-item>
          <el-form-item label="监测数据项" prop="dataItemName"  ref="dataItem">
            <el-select class="w100p" filterable clearable v-model="nowdataitemindex" :disabled="!canInput || nowChanceStau == 'edit'"
              :placeholder="formData.dataItemName" @change="chandataitem">
              <el-option
                :disabled="nowalarmTemplateId ? Number(formData.dataTypeId) !== Number(item.dataType.dataTypeId) ? true : false : false"
                v-show="item.dataType ? item.dataType.mainingControlType == 'COLLECTION' : ''"
                v-for="(item, index) in dataitemlist" :key="index" :value="index" :label="item.dataItemName" />
            </el-select>
          </el-form-item>
          <el-form-item label="发送间隔"  prop="sendingInterval">
            <el-input v-model="formData.sendingInterval" type="number" :disabled="!canInput" clearable min="0"
              @change="handleInput(formData, 'sendingInterval')" />
          </el-form-item>
          <el-form-item label="滤波值"  prop="filterValue">
            <el-input v-model="formData.filterValue" type="number" :disabled="!canInput" clearable min="0"
              @change="handleInput(formData, 'filterValue')" />
          </el-form-item>
          <el-form-item label="滤波类型" >
            <el-select class="w100p" filterable clearable v-model="formData.filterType">
              <el-option v-for="(item, index) in filterTypeList" :key="index" :value="item.name" :label="item.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="上限列表" class="w100p">
            <div>
              <div v-for="(item, index) in formData.nowtopList" :key="index" class="line">
                <el-form-item :prop="'nowtopList.' + index + '.levelName'"
                  :rules="{ required: true, message: '级别名称不能为空', trigger: ['change', 'blur'] }" class="w100p">
                  <el-input v-model="item.levelName" clearable placeholder="级别名称" />
                </el-form-item>
                <el-form-item :prop="'nowtopList.' + index + '.thresholdValue'"
                  :rules="{ required: true, message: '级别阈值不能为空', trigger: ['change', 'blur'] }" class="w100p">
                  <el-input v-model="item.thresholdValue" type="number" min="0" clearable placeholder="级别阈值"
                    @change="handleInput(item, 'thresholdValue')" />
                </el-form-item>
                <el-form-item :prop="'nowtopList.' + index + '.alarmTypeId'"
                  :rules="{ required: true, message: '告警类型不能为空', trigger: ['change', 'blur'] }" class="w100p">
                  <el-select filterable clearable v-model="item.alarmTypeId" placeholder="告警类型">
                    <el-option v-for="(alarmTypeitem, alarmTypeindex) in alarmTypeList" :key="alarmTypeindex"
                      :label="alarmTypeitem.alarmTypeName" :value="alarmTypeitem.alarmTypeId" />
                  </el-select>
                </el-form-item>
                <div class="line-click" v-show="formData.nowtopList.length > 1" @click="deltopList(index)">
                  <i class="el-icon-minus"></i>
                </div>
                <div class="line-click" v-show="formData.nowtopList.length - 1 === index" @click="addtopList()">
                  <i class="el-icon-plus"></i>
                </div>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="下限列表" class="w100p">
            <div>
              <div v-for="(item, index) in formData.nowbottomList" :key="index" class="line">
                <el-form-item :prop="'nowbottomList.' + index + '.levelName'"
                  :rules="{ required: true, message: '级别名称不能为空', trigger: ['change', 'blur'] }" class="w100p">
                  <el-input v-model="item.levelName" clearable placeholder="级别名称" />
                </el-form-item>
                <el-form-item :prop="'nowbottomList.' + index + '.thresholdValue'"
                  :rules="{ required: true, message: '级别阈值不能为空', trigger: ['change', 'blur'] }" class="w100p">
                  <el-input v-model="item.thresholdValue" clearable type="number" min="0" placeholder="级别阈值"
                    @change="handleInput(item, 'thresholdValue')" />
                </el-form-item>
                <el-form-item :prop="'nowbottomList.' + index + '.alarmTypeId'"
                  :rules="{ required: true, message: '告警类型不能为空', trigger: ['change', 'blur'] }" class="w100p">
                  <el-select filterable clearable v-model="item.alarmTypeId" placeholder="告警类型">
                    <el-option v-for="(alarmTypeitem, alarmTypeindex) in alarmTypeList" :key="alarmTypeindex"
                      :label="alarmTypeitem.alarmTypeName" :value="alarmTypeitem.alarmTypeId" />
                  </el-select>
                </el-form-item>
                <div class="line-click" v-show="formData.nowbottomList.length > 1" @click="delbottomList(index)">
                  <i class="el-icon-minus"></i>
                </div>
                <div class="line-click" v-show="formData.nowbottomList.length - 1 === index" @click="addbottomList()">
                  <i class="el-icon-plus"></i>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="cancel">取 消</el-button>
          <el-button class="savebtn" type="primary" @click="save('formData')">保 存</el-button>
        </div>
      </el-dialog>
      <el-dialog :close-on-click-modal='false' :title="'查看基础告警配置'" :visible.sync="dialogeSeeFormVisible" width="50%" >
        <el-form ref="formData" :model="formData" :rules="rules"  label-width="120px" class="demo-ruleForm"
          :disabled="nowChanceStau === ''">
          <el-form-item label="鱼塘选择" prop="fishPondObj" >
            <el-select class="w100p" filterable clearable v-model="formData.fishPondObj" @change="chanbatch" value-key="fishPondId"
              placeholder="请选择鱼塘">
              <el-option v-for="item in fishPondArr" :key="item.fishPondId" :label="item.pondName"
                :value="{ fishPondId: item.fishPondId, pondName: item.pondName }" />
            </el-select>
          </el-form-item>
          <el-form-item label="告警名称"  prop="baseAlarmName">
            <el-input v-model="formData.baseAlarmName" disabled />
          </el-form-item>
          <el-form-item label="监测数据项" >
            <el-select class="w100p" filterable clearable v-model="nowdataitemindex" disabled :placeholder="formData.dataItemName">
              <el-option v-for="(item, index) in dataitemlist" :key="index" :value="index" :label="item.originalName" />
            </el-select>
          </el-form-item>
          <el-form-item label="发送间隔"  prop="sendingInterval">
            <el-input v-model="formData.sendingInterval" disabled clearable />
          </el-form-item>
          <el-form-item label="滤波值"  prop="filterValue">
            <el-input v-model="formData.filterValue" disabled clearable />
          </el-form-item>
          <el-form-item label="滤波类型" >
            <el-select class="w100p" filterable clearable v-model="formData.filterType">
              <el-option v-for="(item, index) in filterTypeList" :key="index" :value="item.name" :label="item.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="上限列表" class="w100p">
            <div>
              <div v-for="(item, index) in formData.nowtopList" :key="index" class="line">
                <el-input class="item" v-model="item.levelName" disabled clearable placeholder="级别名称" />
                <el-input class="item" v-model="item.thresholdValue" disabled clearable placeholder="级别阈值" />
                <el-select class="item" filterable clearable v-model="item.alarmTypeId">
                  <el-option v-for="(alarmTypeitem, alarmTypeindex) in alarmTypeList" :key="alarmTypeindex"
                    :label="alarmTypeitem.alarmTypeName" :value="alarmTypeitem.alarmTypeId" />
                </el-select>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="下限列表" class="w100p">
            <div>
              <div v-for="(item, index) in formData.nowbottomList" :key="index" class="line">
                <el-input class="item" v-model="item.levelName" disabled clearable placeholder="级别名称" />
                <el-input class="item" v-model="item.thresholdValue" disabled clearable placeholder="级别阈值" />
                <el-select class="item" filterable clearable v-model="item.alarmTypeId" disabled>
                  <el-option v-for="(alarmTypeitem, alarmTypeindex) in alarmTypeList" :key="alarmTypeindex"
                    :label="alarmTypeitem.alarmTypeName" :value="alarmTypeitem.alarmTypeId" />
                </el-select>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="() => { dialogeSeeFormVisible = false }">取 消</el-button>
        </div>
      </el-dialog>
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import BtnSlot from '@/components-production-management/BtnSlot';

import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";
import monitoringTemplateApi from "@/api/monitoring-alarm/alarm-template-configuration";
import basicAlarmApi from "@/api/monitoring-alarm/basic-alarm-configuration";
import monitoringCategoryApi from "@/api/monitoring-alarm/alarm-category-management";
import batchApi from "@/api/batch-mange/index";
import fishPondApi from "@/api/basic-info-manage/fishPond";
import dataItemApi from "@/api/basic-info-manage/data-item-manage/index";
export default {
  name: "BasicAlarmConfiguration",
  components: { BtnSlot, Pagination, MainSlot },
  mixins: [mymixin],
  data() {
    return {
      btnList: ['add', 'delete'],
      formData: {
        nowtopList: [{ thresholdType: "UPPERLIMIT" }],
        nowbottomList: [{ thresholdType: "LOWERLIMIT" }]
      },
      rules: {
        baseAlarmName: [
          { required: true, message: "请输入告警名称", trigger: ['change', 'blur'] },
          {
            LOWERLIMIT: 3,
            UPPERLIMIT: 8,
            message: "长度在 3 到 8 个字符",
            trigger: ['change', 'blur']
          }
        ],
        sendingInterval: [
          { required: true, message: "请输入发送间隔", trigger: ['change', 'blur'] }
        ],
        filterValue: [
          { required: true, message: "请输入滤波值", trigger: ['change', 'blur'] }
        ],
        filterType: [
          { required: true, message: "请选择滤波类型", trigger: ['change', 'blur'] }
        ],
        fishPondObj: [
          { required: true, message: "请选择鱼塘", trigger: ['change', 'blur'] }
        ],
        dataItemName: [
          { required: true, message: "请选择数据项", trigger: ['change', 'blur'] }
        ],


      },
      // 数值告警时
      // 数据项
      dataitemtwoindex: "",
      dataitemtwoname: "",
      dataitemtwolist: [],
      // 鱼塘
      facilitytwoIndex: "",
      facilitytwoname: "",
      facilitylist: [],
      // 选择类型
      selectbutlist: [
        { name: "物联自动告警" }
        // { name: "设备异常告警" }
        // { name: "手动记录告警" },
      ],
      nowselecttype: 0,
      // 监测数据项
      nowdataitemindex: "",
      dataitemlist: [],
      // 告警模板
      nowalarmTemplateId: "",
      alarmTemplatelist: [],
      nowbatch: {},
      // 上限列表
      basetopList: {
        levelName: "",
        alarmTypeId: "",
        thresholdType: "UPPERLIMIT",
        thresholdValue: ""
      },
      // 滤波类型
      nowfilterType: {},
      filterTypeList: [
        {
          name: "时间"
        },
        {
          name: "次数"
        }
      ],
      // 告警类型下拉数据
      alarmTypeList: [],
      // 选中的数据项数组 alarmContactId组成 (此数组最多为一个)
      selectIdList: [],
      // 鱼塘数组
      fishPondArr: [],
      dialogTitle: "",
      canInput: false,
      dialogeSeeFormVisible: false,
      dialogFormVisible: false
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {
    nowChanceStau: {
      handler(val) {
        //console.log(val)
        if (val === "add") {
          this.$nextTick(() => {
            this.formData = {
              nowtopList: [{ thresholdType: "UPPERLIMIT" }],
              nowbottomList: [{ thresholdType: "LOWERLIMIT" }],
            }
            this.nowalarmTemplateId = ''
            this.$refs['formData'] && this.$refs['formData'].clearValidate()
          })
          this.nowfilterType = {};
          this.nowdataType = {};
          this.alarmTypeList = [];
        }
      }
    },
    dialogFormVisible: {
      handler(status) {
        if (!status) {
          this.$refs['formData'] && this.$refs['formData'].resetFields()
        }
      }
    }
  },
  created() {
    // 获取表格数据
    this.search();
  },
  mounted() {
    this.getselectlist();
  },
  methods: {
    handleInput(data, name) {
      data[name] < 0 && (data[name] = '')
    },
    // 获取表格数据
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize
      };
      basicAlarmApi.getlist(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
      });
    },
    // 获取下拉数据
    async getselectlist() {
      // 获取可选择模板
      monitoringTemplateApi.queryByCondition().then(res => {
        this.alarmTemplatelist = res.data;
        if (this.alarmTemplatelist[0] && this.alarmTemplatelist[0].alarmTemplateId) this.chancetemple(this.alarmTemplatelist[0].alarmTemplateId);
      });
      // 获取所有监测数据项
      dataItemApi.queryByCondition().then(res => {
        this.dataitemlist = res.data;
        console.log(this.formData)
      });
      // 获取鱼塘
      const { data: resp } = await fishPondApi.queryByCondition({});
      this.fishPondArr = resp||[];
      console.log(this.fishPondArr)
    },
    // ----
    // 获取不同类型的表格数据
    gettabletype(i) {
      this.nowselecttype = i;
      this.formData = {};
      if (i === 0) {
        this.search();
      } else if (i === 1) {
        // this.getTableDataListtwo();
      } else if (i === 2) {
        // this.getTableDataListtwo()
      }
    },
    // 选择鱼塘
    chanbatch(e) {
      // this.formData.mnecode = e.name
      // this.formData.productionBatchId = e.value
      this.$set(this.formData, "fishPondId", e.fishPondId);
      this.$set(this.formData, "fishPondName", e.pondName);
      // this.formData.dataItemName = null
      if (e.fishPondId) {
        // // 获取所有监测数据项
        dataItemApi.queryByCondition({ fishPondId: e.fishPondId }).then(res => {
          console.log('---', res)
          this.dataitemlist = res.data;
          this.nowdataitemindex = ''
          this.dataitemlist.forEach(item => {
            if (item.dataTypeId == this.formData.dataTypeId) {
              this.formData.dataItemName = item.dataItemName ? item.dataItemName : '请选择数据项'
            }
          })
        });
      }
    },
    // 选择模板
    chancetemple(e) {
      monitoringTemplateApi.getlist({
        page: 1,
        size: 10,
        entity: {
          homeBaseId: sessionStorage.homeBaseId,
          alarmTemplateId: e
        }
      }).then(res => {
        let row = res.data.content[0];
        console.log(row)
        // this.formData.dataItemName =
        if (this.nowalarmTemplateId > 0) {
          this.chancetemplelist(row);
        }
      });
    },
    // 选择数据项
    chandataitem(i) {
      // debugger
      //console.log(i, this.dataitemlist[i]);
      this.formData["dataItemName"] = this.dataitemlist[i].dataItemName;
      this.formData["dataItemId"] = this.dataitemlist[i].dataItemId;
      this.formData["iotDataItemId"] = this.dataitemlist[i].iotDataItemId;

      //console.log(this.dataitemlist[i])
      if (this.dataitemlist[i].dataType) {
        this.formData["dataTypeName"] = this.dataitemlist[i]['dataType'].dataTypeName;
        this.formData["dataTypeId"] = this.dataitemlist[i]['dataType'].dataTypeId;
        monitoringCategoryApi.queryByCondition({
          dataTypeId: this.dataitemlist[i]['dataType'].dataTypeId,
          homeBaseId: sessionStorage.homeBaseId
        })
          .then(res => {
            let data = res.data;
            this.chancealarmtype(data);
            if (!this.nowalarmTemplateId) {
              this.$set(this.formData, 'nowtopList', [{ thresholdType: "UPPERLIMIT" }])
              this.$set(this.formData, 'nowbottomList', [{ thresholdType: "LOWERLIMIT" }])
            }
          });
      }
      setTimeout(() => { this.$refs['dataItem'].clearValidate() }, 100)
    },
    // 选择模板时的数据处理
    chancetemplelist(zhrow) {
      this.formData = {};
      let row = JSON.parse(JSON.stringify(zhrow));
      if (row.alarmTemplateId) {
        this.$set(this.formData, 'alarmTemplateId', row.alarmTemplateId)
        this.$set(this.formData, 'templateName', row.templateName)
        this.$set(this.formData, 'sendingInterval', row.sendingInterval)
        this.$set(this.formData, 'filterValue', row.filterValue)
        this.$set(this.formData, 'filterType', row.filterType)
        this.$set(this.formData, 'filterType', row.filterType)
        this.$set(this.formData, 'noticeConfigNo', row.noticeConfigNo)
        this.$set(this.formData, 'dataTypeId', row.dataTypeId)
        this.$set(this.formData, 'dataTypeName', row.dataTypeName)
        this.$set(this.formData, 'nowtopList', [])
        this.$set(this.formData, 'nowbottomList', [])
        monitoringCategoryApi.queryByCondition({
          dataTypeId: zhrow.dataTypeId,
          homeBaseId: sessionStorage.homeBaseId
        })
          .then(res => {
            let data = res.data;
            this.chancealarmtype(data);
          });
        let mainlist = row.alarmTemplateThresholdList;
        // this.formData.nowtopList = [];
        // this.formData.nowbottomList = [];
        this.$set(this.formData, 'nowtopList', [])
        this.$set(this.formData, 'nowbottomList', [])
        if (mainlist !== null) {
          for (let i = 0; i < mainlist.length; i++) {
            if (mainlist[i].thresholdType === "UPPERLIMIT") {
              this.$set(this.formData.nowtopList, 0, mainlist[i])
              // this.formData.nowtopList.push(mainlist[i]);
            } else if (mainlist[i].thresholdType === "LOWERLIMIT") {
              this.$set(this.formData.nowbottomList, 0, mainlist[i])
              // this.formData.nowbottomList.push(mainlist[i]);
            }
          }
        }
      }
    },
    // 添加一条上限数据
    addtopList() {
      if (this.nowChanceStau !== "add" && this.nowChanceStau !== "edit") {
        return;
      }
      let addvalue = {
        thresholdType: "UPPERLIMIT",
      };
      this.formData.nowtopList.push(addvalue);
    },
    // 删除一条上限数据
    deltopList(i) {
      if (this.nowChanceStau !== "add" && this.nowChanceStau !== "edit") {
        return;
      }
      this.formData.nowtopList.splice(i, 1);
    },
    // 添加一条下限数据
    addbottomList() {
      if (this.nowChanceStau !== "add" && this.nowChanceStau !== "edit") {
        return;
      }
      let addvalue = {
        thresholdType: "LOWERLIMIT",
      };
      this.formData.nowbottomList.push(addvalue);
    },
    // 添加一条下限数据
    delbottomList(i) {
      if (this.nowChanceStau !== "add" && this.nowChanceStau !== "edit") {
        return;
      }
      this.formData.nowbottomList.splice(i, 1);
    },
    dbHandleSee(row) {
      this.dialogeSeeFormVisible = true;
    },
    // 通过鱼塘id获取数据dataitem
    getbyconditiondataitem(par) {
      basicAlarmApi.getdataitemlist(par).then(res => {
        this.dataitemtwolist = res.data;
      });
    },
    // 将选择的数据类别添加到表单中 --获取告警类型列表
    addDataType(e) {
      if (this.nowChanceStau === "add" || this.nowChanceStau === "edit") {
        this.formData["dataTypeId"] = e.value;
        this.formData["dataTypeName"] = e.name;
      }
      //console.log(e)
      monitoringCategoryApi
        .queryByCondition({
          dataTypeId: e.value,
          homeBaseId: sessionStorage.homeBaseId
        })
        .then(res => {
          let data = res.data;
          this.chancealarmtype(data);
        });
    },
    // 处理数据类型数据
    chancealarmtype(list) {
      let newlist = [];
      list.forEach((item, index) => { // 不理解这么写的意义是什么
        newlist.push(item);
      });
      this.alarmTypeList = newlist;
    },
    handleEdit(zhrow) {
      this.formData = {};
      let row = { ...zhrow };
      // 下面时物联自动告警
      if (row.baseAlarmId) {
        this.formData = {
          baseAlarmId: row.baseAlarmId,
          baseAlarmName: row.baseAlarmName,
          sendingInterval: row.sendingInterval,
          filterValue: row.filterValue,
          filterType: row.filterType,
          dataItemName: row.dataItemName,
          dataItemId: row.dataItemId,
          iotDataItemId: row.iotDataItemId,
          dataTypeName: row.dataTypeName,
          dataTypeId: row.dataTypeId,
          productionBatchId: row.productionBatchId,
          mnecode: row.mnecode,
          nowalarmTemplateId: row.nowalarmTemplateId,
          noticeConfigNo: row.noticeConfigNo,
          fishPondObj: {
            fishPondId: row.fishPondId,
            pondName: row.fishPondName
          }
        };
        this.formData.fishPondObj = this.fishPondArr.filter(item => { return Number(item.fishPondId) == Number(row.fishPondId) })[0]

        this.nowalarmTemplateId = row.nowalarmTemplateId;

        this.addDataType({ name: row.dataTypeName, value: row.dataTypeId });
        let mainlist = row.baseAlarmThresholdList;
        this.formData.nowtopList = [];
        this.formData.nowbottomList = [];
        if (mainlist !== null) {
          for (let i = 0; i < mainlist.length; i++) {
            if (mainlist[i].thresholdType === "UPPERLIMIT") {
              this.formData.nowtopList.push(mainlist[i]);
            } else if (mainlist[i].thresholdType === "LOWERLIMIT") {
              this.formData.nowbottomList.push(mainlist[i]);
            }
          }
        }
      }
      // this.openDialog('edit');
    },
    handleSelectionChange(list) {
      this.selectIdList = [];
      let notvalue = true;
      if (this.nowselecttype === 1) {
        this.selectIdList = list.map(item => {
          if (item.numericalAlarmId) {
            return item.numericalAlarmId;
          } else {
            notvalue = false;
            return "";
          }
        });
      } else if (this.nowselecttype === 0) {
        this.selectIdList = list.map(item => {
          if (item.baseAlarmId) {
            return item.baseAlarmId;
          } else {
            notvalue = false;
            return "";
          }
        });
      }
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.selectIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },

    //获取选中Btn
    getBtnEvent(flag) {
      if (flag == 'add') {
        this.add();
      } else if (flag == 'delete') {
        this.delmessage()
      }
    },
    delmessage() {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",

      })
        .then(() => {
          basicAlarmApi.dellist(this.selectIdList).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.search();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });

    },
    handleDelete(data) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",

      })
        .then(() => {
          basicAlarmApi.dellist([data.baseAlarmId]).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.search();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          // debugger
          //console.log(this.formData)
          if (this.nowChanceStau === "edit") {
            let editparams = {
              baseAlarmThresholdList: [],
              ...this.formData
            };
            editparams.baseAlarmThresholdList = [...this.formData.nowbottomList, ...this.formData.nowtopList]
            editparams.dataItemId = editparams.iotDataItemId
            delete editparams.nowbottomList
            delete editparams.nowtopList
            // 编辑接口
            basicAlarmApi.editlist(editparams).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.closeDialog();
                this.search();
              } else {
                this.$message.error("修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            let addparams = {
              baseAlarmThresholdList: [],
              ...this.formData
            };
            addparams.baseAlarmThresholdList = [...this.formData.nowbottomList, ...this.formData.nowtopList]
            addparams.dataItemId = addparams.iotDataItemId
            delete addparams.nowbottomList
            delete addparams.nowtopList
            setTimeout(() => {
              basicAlarmApi.addlist(addparams).then(res => {
                if (res.success) {
                  this.$message.success("新增成功！");
                  this.closeDialog();
                  this.search();
                } else {
                  this.$message.error(res.message || "新增失败！");
                }
              });
            }, 200);
          }
        } else {
          //console.log('error submit!!');
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
      this.formData = {};
      this.nowbatch = "";
      this.nowdataitemindex = "";
      this.basetopList = {
        levelName: "",
        alarmTypeId: "",
        thresholdType: "UPPERLIMIT",
        thresholdValue: ""
      };
      this.nowalarmTemplateId = "";
      this.nowfilterType = "";
    }
  }
};
</script>

<style scoped lang="scss">
.line {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
  margin-bottom: 10px;
  .item {
    width: calc(100% / 3);
  }

  .line-click {
    width: 90px;
    height: 30px;
    // background-color: aqua;
    display: flex;
    justify-content: center;
    align-items: center;
    border: snow 1px solid;
    border-radius: 50%;
    margin-bottom: 10px;
    margin-top: 5px;
    margin-left: 5px;
    cursor: pointer;
  }
}

::v-deep .tableSlot {
  height: 100%;
}
</style>
